<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">

        <el-form-item>
          <span>状态：</span>
          <el-radio-group v-model="selectOptions.status" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in statusMaps" :label="item.value" name="status" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <span>身份：</span>
          <el-radio-group v-model="selectOptions.farm_status" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in cardMaps" :label="item.value" name="farm_status" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <span>设备类型：</span>
          <el-radio-group v-model="selectOptions.os_type" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in phonetype" :label="item.value" name="os_type" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>

        <div style="display: inline-block;float: right">
          <el-select v-model="selectOptions.query" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-form-item>
            <el-input :placeholder="selectOptions.query" v-model="selectOptions.keywords" @keyup.enter.native="doSearch"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="doSearch">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading"
              style="width: 100%;" element-loading-text="玩命请求中。。。">
      <el-table-column type="selection" width="80">
      </el-table-column>
      <el-table-column label="uid" width="80" prop="uid">
      </el-table-column>
      <el-table-column label="用户头像" width="120">
        <template slot-scope="scope">
          <div class="user-avatar" v-if="scope.row.avatar">
            <a :href="scope.row.avatar" target="_blank"><img :src="scope.row.avatar" alt=""></a>
          </div>
          <div class="user-avatar" v-else>
            <img src="../../../static/images/default.png" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="150">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="150">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="100" :formatter="formatSex">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100" :formatter="userStatus">
      </el-table-column>
      <el-table-column prop="os_type" label="设备类型" width="120" :formatter="osType">
      </el-table-column>
      <el-table-column prop="farm_status" label="身份" width="120" :formatter="farmStatus">
      </el-table-column>
      <el-table-column prop="from" label="来源" width="120" :formatter="changeFrom">
      </el-table-column>
      <el-table-column prop="regtime" label="注册时间" width="190" :formatter="regTime">
      </el-table-column>
      <el-table-column prop="update_time" label="登录时间" width="190">
      </el-table-column>
      <el-table-column label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button type="danger" size="small" @click="setUserStatus(scope.row.uid, 1)" v-if="scope.row.status === '1'">锁定</el-button>
          <el-button type="success" size="small" @click="setUserStatus(scope.row.uid, 2)" v-else>解锁</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>

<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
  import { apiTree } from '../../apiTree'

  export default {
    data() {
      return {
        options: [
          {value: 'uid', label: '用户uid'},
          {value: 'phone', label: '手机号'},
          {value: 'nickname', label: '昵称'},
          {value: 'from', label: '渠道'}
        ],
        statusMaps: [
          {value: -1, label: '全部'},
          {value: 1, label: '正常'},
          {value: 0, label: '锁定'}
        ],
        cardMaps: [
          {value: -1, label: '所有'},
          {value: 1, label: '农场主'},
          {value: 0, label: '普通用户'}
        ],
        phonetype: [
        	{value: -1, label: '所有'},
        	{value: 1, label: '安卓'},
        	{value: 2, label: 'IOS'}
        ],
        users: [],
        total: 0,
        listLoading: false,
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          query: this.$route.query.query !== undefined ? this.$route.query.query : '',
          keywords: this.$route.query.keywords !== undefined ? this.$route.query.keywords : '',
          status: this.$route.query.status !== undefined ? this.$route.query.status : -1,
          farm_status: this.$route.query.farm_status !== undefined ? this.$route.query.farm_status : -1,
          os_type: this.$route.query.os_type !== undefined ? this.$route.query.os_type : -1
        }
      }
    },
    computed: {},
    methods: {
      // 设备类型
      osType(row) {
        return row.os_type === '1' ? '安卓' : row.os_type === '2' ? 'IOS' : '未知'
      },
      farmStatus(row) {
        return row.farm_status === '1' ? '农场主' : '普通用户'
      },
      changeFrom(row) {
        return row.from === null ? '无' : row.from
      },
      // 性别显示转换
      formatSex: function (row, column) {
        return row.sex === '1' ? '男' : row.sex === '2' ? '女' : '未知'
      },
      // 状态转换
      userStatus(row, column) {
        return row.status === '0' ? '锁定' : '正常'
      },
      // 最后修改土地时间
      updateFieldNameTime(row, column) {
        return util.formatDate.format(new Date(row.last_update_field_name_time * 1000), 'yyyy-MM-dd hh:mm:ss')
      },
      // 注册时间
      regTime(row, column) {
        return util.formatDate.format(new Date(row.regtime * 1000), 'yyyy-MM-dd hh:mm:ss')
      },
      userPosition(row, column) {
        return row.farm_status === '1' ? '农场主' : '普通用户'
      },
      // 改变当前状态
      currentStatusChange() {
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      currentPageChange(val) {
        this.selectOptions.page = val
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      doSearch() {
        if (this.selectOptions.query) {
          this.$router.push({path: this.$route.path, query: this.selectOptions})
        }
      },
      // 锁定用户
      setUserStatus(uid, _type) {
        this.$confirm('确定进行该操作吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true
          NProgress.start()
          let param = {ids: uid, type: _type}
          apiTree('user/index', 'user/setUserStatus', param).then((res) => {
            NProgress.done()
            if (res.s === 1) {
              this.listLoading = false
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.getUsers()
            }
          })
        }).catch(() => {

        })
      },
      _reload() {
        this.getUsers()
      },
        // 获取用户列表
      getUsers() {
        let param = this.$route.query
        if (util.isEmptyObject(param)) {
          this.selectOptions = {
            page: 1,
            query: '',
            keywords: '',
            status: -1,
            farm_status: -1,
            os_type: -1
          }
        }
        this.listLoading = true
        NProgress.start()
        apiTree('user/index', 'user/index', param).then((res) => {
          this.users = res.d.data
          this.total = res.d.total_num
          this.listLoading = false
          NProgress.done();
        })
      }
    },
    created() {
      this.getUsers()
    },
    mounted() {
//      console.log(this.selectOptions)
    },
    watch: {
      $route: 'getUsers'
    }
  }

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .user-avatar
    width 60px
    height 60px
    border-radius 50%
    & img
      width 100%
      height 100%
      border-radius 50%
  .cell {
    overflow: hidden;
  }
  .name {
    display: block;
    padding-top: 10px;
  }
  .people {
    float: right;
  }
  .user-avatar {
    float: left;
  }
</style>
